
/**
 * 创建浮点数据的数组缓冲
 * @param {WebGL对象} gl 
 * @param {缓冲数据} data 
 * @param {每个分量的长度} itemSize 
 * @param {要激活的着色器顶点属性} attribute 
 */
function createArraysBuffer(gl, data, itemSize, attribute){
    var buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW);
    var position = gl.getAttribLocation(gl.program, attribute);
    if(position<0){
        console.log("着色器属性未找到!");
        return;
    }
    gl.vertexAttribPointer(position, itemSize, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(position);
}
/**
 * 创建字节数据的数组缓冲
 * @param {WebGL对象} gl 
 * @param {缓冲数据} data 
 * @param {每个分量的长度} itemSize 
 * @param {要激活的着色器顶点属性} attribute 
 */
function createArraysByteBuffer(gl, data, itemSize, attribute){
    var buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(data), gl.STATIC_DRAW);
    var position = gl.getAttribLocation(program, attribute);
    console.log(position);
    if(position<0){
        console.log("着色器属性未找到!");
        return;
    }
    gl.vertexAttribPointer(position, itemSize, gl.UNSIGNED_BYTE, false, 0, 0);
    gl.enableVertexAttribArray(position);
}
/**
 * 创建元素数组缓冲
 * @param {WenGL对象} gl 
 * @param {元素数组缓冲数据} data 
 */
function createElementsBuffer(gl, data){
    var eleBuff = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, eleBuff);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint8Array(data), gl.STATIC_DRAW);
}
/**
 * 加载2D纹理资源
 * @param {WebGL对象} gl 
 * @param {纹理图像url} src 
 */
function loadTexture(gl, src){
    var img = new Image();
    img.onload = function(){
        var texture = gl.createTexture();
        if(!texture){
            console.log("创建纹理失败!");
            return;
        }
        gl.bindTexture(gl.TEXTURE_2D, texture);
        gl.pixelStoei(gl.UNPACK_FLIP_Y_WEBGL, true);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.UNSIGNED_BYTE, img);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.bindTexture(gl.TEXTURE_2D, null);
    }
    img = src;
}

export {createArraysBuffer, createArraysByteBuffer, createElementsBuffer};